#include"comm.hpp"
#include"log.hpp"
using namespace std;

//利用服务端 — 建立一个管道文件

#define SIZE 1024 //大小
//服务端 — 读取信息
int main()
{
    Log log;
    //log.set(OneFile);
    log.set(ClassFile);
    Init init; // RAII对象管理 — 管道文件的开关由生命周期管理
    
    int fd = open(PATHFILE, O_RDONLY); //以只读的方式打开，等待写入方打开之后，自己才会打开文件，向后执行， open 阻塞了！
    if(fd < 0)
    {
        log(Fatal, "error string: %s, error code: %d", strerror(errno), errno);
        exit(FILE_OPEN_ERROR);
    }
    //测试：
    log(Info, "server open file done, error string: %s, error code: %d", strerror(errno), errno);
    log(Warning, "server open file done, error string: %s, error code: %d", strerror(errno), errno);
    log(Fatal, "server open file done, error string: %s, error code: %d", strerror(errno), errno);
    log(Debug, "server open file done, error string: %s, error code: %d", strerror(errno), errno);

    //开始通信
    char buffer[SIZE] = {0};
    while(true)
    {
        buffer[0] = 0;

        int len = read(fd, buffer, sizeof(buffer));
        if(len > 0)
        {
            buffer[len] = 0;
            cout << "get a message from cilent: " << buffer << endl;
        }
        else if(len == 0)
        {
            cout << "cilent quit, me(severs) quit too" << endl;
            break;
        }
        else break;
    }
    sleep(5); //调试代码

    close(fd);
    return 0;
}